import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import ddddocr
from app.exchange import exchange_proxy, check_code

def mgtv_invoke(country_code, phone_num, phone_num_url, exchange_code):
    proxy_info = exchange_proxy.proxy_method();
    options = webdriver.FirefoxOptions()
    options.add_argument(f'--proxy-server=http://{proxy_info[0]}:{proxy_info[1]}')

    driver = webdriver.Firefox(options=options)
    driver.get("https://i.mgtv.com/vip/exchange/page")
    driver.maximize_window()

    # 登陆弹窗
    time.sleep(2)
    driver.find_element(By.ID, 'honey-header-user').click()

    # 选择短信登陆
    time.sleep(2)
    driver.find_element(By.XPATH, "//li[@node-type='honey-mini-login-mes-btn']").click()

    # 填写手机号
    time.sleep(1)
    phone_input = driver.find_element(By.XPATH, "//input[@node-type='honey-mini-mes-phone']")
    phone_input.click()
    phone_input.send_keys(phone_num)

    # 填写验证码
    image_element = driver.find_element(By.ID, 'login-yzm_img')
    image_bytes = image_element.screenshot_as_png
    ocr = ddddocr.DdddOcr()
    result = ocr.classification(image_bytes)
    print(result)
    if len(result) != 4:
        image_element.click()
        image_element = driver.find_element(By.ID, 'login-yzm_img')
        image_bytes = image_element.screenshot_as_png
        ocr = ddddocr.DdddOcr()
        result = ocr.classification(image_bytes)
        print(result)

    login_verify_code = driver.find_element(By.XPATH, "//input[@node-type='meslogin-code']")
    login_verify_code.click()
    time.sleep(1)
    login_verify_code.send_keys(result)

    # 点击获取验证码
    time.sleep(1)
    driver.find_element(By.CLASS_NAME, 'verif-a').click()

    # 获取验证码
    time.sleep(3)
    phone_code = check_code.get_check_code(phone_num_url)

    # 填写验证码
    time.sleep(1)
    code_input = driver.find_element(By.XPATH, "//input[@node-type='honey-mini-mes-code']")
    code_input.click()
    time.sleep(1)
    code_input.send_keys(phone_code)

    # 点击登陆
    time.sleep(1)
    driver.find_element(By.ID, 'honey-mini-mes-login-submit').click()

    # 输入兑换码
    time.sleep(1)
    WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME, 'inputcon')))
    redeem_input = driver.find_element(By.ID, 'exchangecode')
    redeem_input.click()
    time.sleep(1)
    redeem_input.send_keys(exchange_code)

    exchange_verify_code = driver.find_element(By.ID, 'vertifycode')
    image_element = driver.find_element(By.XPATH, '//*[@id="vercode"]')
    image_bytes = image_element.screenshot_as_png
    ocr = ddddocr.DdddOcr()
    result = ocr.classification(image_bytes)
    if len(result) != 4:
        print('识别有问题')
        image_element.click()
        image_element = driver.find_element(By.XPATH, '//*[@id="vercode"]')
        image_bytes = image_element.screenshot_as_png
        result = ocr.classification(image_bytes)
    exchange_verify_code.click()
    time.sleep(1)
    exchange_verify_code.send_keys(result)

    # 点击兑换
    time.sleep(2)
    exchange_button = driver.find_element(By.CLASS_NAME, 'kq-btn')
    exchange_button.click()

    # 退出
    time.sleep(2)
    driver.quit()

if __name__ == '__main__':
    mgtv_invoke('+86', '18633448636', '1', 'xxxx-mgtv-1234-month1')

